package com.nlx.notes.module.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nlx.notes.module.bean.dto.FlowSaveDTO;
import com.nlx.notes.module.bean.dto.flow.UpdateDBAmountDTO;
import com.nlx.notes.module.core.enums.TransactionTypeEnum;
import com.nlx.notes.module.core.util.AmountConverterUtils;
import com.nlx.notes.module.core.util.DateUtils;
import com.nlx.notes.module.entity.FlowYearStatistics;
import com.nlx.notes.module.mapper.FlowYearStatisticsMapper;
import com.nlx.notes.module.service.IFlowYearStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author nlx
 * @since 2024-12-15
 */
@Service
public class FlowYearStatisticsServiceImpl extends ServiceImpl<FlowYearStatisticsMapper, FlowYearStatistics> implements IFlowYearStatisticsService {

    @Override
    public FlowYearStatistics findByYear(String year,Long bookId,Long userId) {
        if(StrUtil.isEmpty(year)){
            return null;
        }
        return getBaseMapper().selectOne(Wrappers.lambdaQuery(FlowYearStatistics.class)
                .eq(FlowYearStatistics::getUserId,userId)
                        .eq(FlowYearStatistics::getBookId,bookId)
                .eq(FlowYearStatistics::getYearTime,year)
                .eq(FlowYearStatistics::getDelFlag,0),false);
    }

    @Override
    public void saveYearFlow(FlowSaveDTO flowSaveDTO) {
        String year = DateUtils.formatCurrentYear(flowSaveDTO.getTransactionDate());
        int amount = AmountConverterUtils.convertYuanToFen(flowSaveDTO.getAmount());
        FlowYearStatistics statistics = this.findOneByYear(year,flowSaveDTO.getUserId());
        if(null == statistics){
            FlowYearStatistics flowYearStatistics = new  FlowYearStatistics();
            if(TransactionTypeEnum.INCOME.getCode().equalsIgnoreCase(flowSaveDTO.getTransactionType())){
                flowYearStatistics.setIncome(amount);
            }else{
                flowYearStatistics.setExpense(amount);
            }
            flowYearStatistics.setYearTime(year);
            flowYearStatistics.setTransactionType(flowSaveDTO.getTransactionType());
            flowYearStatistics.setUserId(flowSaveDTO.getUserId());
            flowYearStatistics.setDelFlag(0);
            this.save(flowYearStatistics);
        }else{
            this.incYearAmount(statistics.getId(),amount, flowSaveDTO.getTransactionType());
        }
    }

    @Override
    public void deleteFlow(UpdateDBAmountDTO updateDBAmountDTO) {
        String year = DateUtils.formatCurrentYear(updateDBAmountDTO.getDate());
        FlowYearStatistics flowDayStatistics = this.findByDayAndType(year,updateDBAmountDTO.getUserId());
        if(null == flowDayStatistics){
            return ;
        }
        updateDBAmountDTO.setId(flowDayStatistics.getId());
        incYearAmount(flowDayStatistics.getId(),updateDBAmountDTO.getAmount(),updateDBAmountDTO.getTransactionType());
        //通过天查询数据
//        this.getBaseMapper().decYearAmount(flowDayStatistics.getId(),updateDBAmountDTO.getAmount());
    }

    @Override
    public List<FlowYearStatistics> findAllByUserId(Long userId,Long bookId) {
        return getBaseMapper().findAllByUserId(userId,bookId);
    }

    private FlowYearStatistics findByDayAndType(String date,  Long userId) {
        if(StrUtil.isEmpty(date) || null == userId ){
            return null;
        }
        return this.getBaseMapper().selectOne(Wrappers.lambdaQuery(FlowYearStatistics.class)
                .eq(FlowYearStatistics::getUserId,userId)
                .eq(FlowYearStatistics::getYearTime,date)
                .eq(FlowYearStatistics::getDelFlag,0).last(" limit 1"));
    }

    private void incYearAmount(Long id, int amount,String transactionType) {

        if(id == null || 0 == amount){
            return ;
        }
        if(TransactionTypeEnum.INCOME.getCode().equalsIgnoreCase(transactionType)){
            this.getBaseMapper().incYearIncomeAmount(id,amount);
        }else{
            this.getBaseMapper().incYearExpenseAmount(id,amount);
        }
    }


    /**
     * 通过年份查询数据
     * @param year 年份
     * @param userId 用户
     * @return FlowYearStatistics
     */
    private FlowYearStatistics findOneByYear(String year, Long userId) {
        if(null == year || null == userId){
            return null;
        }
        return this.getBaseMapper().selectOne(Wrappers.lambdaQuery(FlowYearStatistics.class)
                .eq(FlowYearStatistics::getUserId,userId)
                .eq(FlowYearStatistics::getYearTime,year)
//                .eq(FlowYearStatistics::getTransactionType,transactionType)
                .eq(FlowYearStatistics::getDelFlag,0)
                .last(" limit 1"));
    }
}
